package com.ywu.bpmn.flow.consumer.process;

import com.ywu.bpmn.flow.consumer.YwuFlowConsumer;
import com.ywu.bpmn.flow.data.YwuFlowInstanceRepository;
import com.ywu.bpmn.flow.entity.YwuFlowInstanceEntity;
import com.ywu.bpmn.spi.dto.YwuBpmnMqDto;
import com.ywu.common.gson.GsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName YwuFLowProcessStartConsumer
 * @Description TODO
 * @Author GroundDemo
 * @Date 2025/4/23 20:46
 * @Version 1.0
 **/
@Component
@Slf4j
public class YwuFLowProcessStartConsumer implements YwuFlowConsumer {

    @Autowired
    private YwuFlowInstanceRepository repository;

    private static final String PROCESS_START = "process-start";

    @Override
    public String type() {
        return PROCESS_START;
    }

    @Override
    public void process(String key, String value) {
        log.debug("do consumer process start");
        // 将json串反序列化
        YwuBpmnMqDto deserialize = GsonUtils.deserialize(value, YwuBpmnMqDto.class);
        // 修改流程实例的状态为RUNNING
        YwuFlowInstanceEntity ywuFlowInstanceEntity = new YwuFlowInstanceEntity();
        ywuFlowInstanceEntity.setId(deserialize.getInstanceId());
        ywuFlowInstanceEntity.setStatus(YwuFlowInstanceEntity.RUNNING_STATUS);
        ywuFlowInstanceEntity.setBpmnInstanceId(deserialize.getBpmnInstanceId());
        repository.updateFlowStatus(ywuFlowInstanceEntity);
    }
}
